<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_attendance extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$data = array();
		//var $data1;
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		 $this->load->model('hrm/attendance_model');			
	}
	
	
   function index()
   {
	  $meta['page_title'] = "Attendance";
	//  $meta[] = $data;
	//$meta['message'] = "Logged In Successfully";
	 
	  $data['server_time'] = $this->attendance_model->get_server_time();
	  
      $this->load->view('common/header', $meta);
      $this->load->view('hrm/attendance',$data);
      $this->load->view('common/footer');
   }
   
   function get_month_id()
	{
	return date('m');
	}
	
	function get_year_id()
	{
	return date('y');
	}
	
	function get_date_id()
	{
	return date('d');
	}
	
	/*Enter Entry Time for attendance */	
	function insert_entry_time()
	{	
	$insert['month_id'] = $this->get_month_id();
	$insert['date_id'] = $this->get_date_id();	
	$insert['user_id'] = $this->session->userdata('user_id');
	$insert['entry_time'] =  $_REQUEST['time'];
	$insert['entry_ip'] = $_SERVER['REMOTE_ADDR'];		
	 echo $this->attendance_model->insert_entry_time($insert);
	}	
	
	function check_entry_time()
	{
	$check['month_id'] = $this->get_month_id();
	$check['date_id'] = $this->get_date_id();	
	$check['user_id'] = $this->session->userdata('user_id');
	echo $this->attendance_model->check_entry_time($check);	
	}
	
	/*Enter Exit Time for attendance */
	function insert_exit_time()
	{			
	$exit['month_id'] = $this->get_month_id();
	$exit['date_id'] = $this->get_date_id();	
	$exit['user_id'] = $this->session->userdata('user_id');
	$exit['exit_time'] =  $_REQUEST['time'];
	echo $this->attendance_model->insert_exit_time($exit);	
	}
	
	function check_exit_time()
	{
	$check['month_id'] = $this->get_month_id();
	$check['date_id'] = $this->get_date_id();	
	$check['user_id'] = $this->session->userdata('user_id');
	echo $this->attendance_model->check_exit_time($check);	
	}
	
	function get_total_working_hours()
  {	
	  $check = array(
		 'month_id' => $this->get_month_id(),
		 'date_id' => $this-> get_date_id(),
		 'user_id' => $this->session->userdata('user_id')
			);
	//print_r($data);
	 $entry_time =  $this->attendance_model->check_entry_time($check);
	 $exit_time = $this->attendance_model->check_exit_time($check);	
	 $entry_time = strtotime($entry_time);	
	 $exit_time = strtotime($exit_time);
	
		$total_time = $exit_time - $entry_time;
		$hours = floor($total_time /3600);     
		$minutes = intval(($total_time/60) % 60);
		$hours.":".$minutes;	
		return $hours.":".$minutes;		
	// $this->load->view('user/dashboard',$time,true);
  }
  
  function insert_daily_attendance_sheet()
  {
	  
	  $date_column = "day_".$this->get_date_id();
	  $total_time = $this->get_total_working_hours();
	 
	  $data = array(
	  'year_id'=>$this->get_year_id(),
	  'month_id'=>$this->get_month_id(),
	  'user_id'=>$this->session->userdata('user_id'),
	  $date_column => $total_time
	  );
	  
	  $sundays = $this->get_sunday();
	  
	  //$holidays = ;
	 
	  echo $this->attendance_model->insert_daily_attendance_sheet($data,$sundays);
  }
  
  function get_attendance_details()
  {
	echo($this->attendance_model->get_attendance_details());
  }
  
  
  
  function get_sunday()
  {
	 $now = new DateTime();
	 $start = $now->format('Y-m-01');
	 $end = $now->format('Y-m-t');
 	$startDate =  strtotime($start);
    $endDate = strtotime($end);
	$weekdayNumber = 0;
    $dateArr = array();
    do
    {
        if(date("w", $startDate) != $weekdayNumber)
        {
            $startDate += (24 * 3600); // add 1 day
        }
    } while(date("w", $startDate) != $weekdayNumber);


    while($startDate <= $endDate)
    {
        $dateArr[] = date('d', $startDate);
        $startDate += (7 * 24 * 3600); // add 7 days
    }

    return($dateArr);
	  
	  //echo($this->attendance_model->get_sundays());
  }
	function get_salary_by_id()
	{
	$q = $this->attendance_model->get_salary_by_id();
	//print_r($q);
		
	}
}
	
	?>